import request, { PageQuery, PageResponse } from './request';

// 用户实体
export interface SysUser {
  id?: number;
  username: string;
  password?: string;
  realName: string;
  mobile: string;
  email: string;
  status: number;
  deptId?: number;
  roleIds?: number[];
  createTime?: string;
  updateTime?: string;
}

// 修改密码请求
export interface ChangePasswordRequest {
  userId: number;
  oldPassword: string;
  newPassword: string;
}

// 分配角色请求
export interface AssignRolesRequest {
  userId: number;
  roleIds: number[];
}

export interface UserQuery extends PageQuery {
  username?: string;
  realName?: string;
  email?: string;
  mobile?: string;
  status?: number;
}

export interface AssignUserPermissionsRequest {
  userId: number;
  permissionIds: number[];
}

// 用户管理API
export const userApi = {
  // 分页查询用户列表
  pageUsers: (params: PageQuery): Promise<PageResponse<SysUser>> => {
    return request.get('/system/user/page', { params });
  },

  // 根据ID查询用户
  getUser: (id: number): Promise<SysUser> => {
    return request.get(`/system/user/${id}`);
  },

  // 创建用户
  createUser: (data: SysUser): Promise<string> => {
    return request.post('/system/user', data);
  },

  // 更新用户
  updateUser: (data: SysUser): Promise<string> => {
    return request.put('/system/user', data);
  },

  // 删除用户
  deleteUser: (id: number): Promise<string> => {
    return request.delete(`/system/user/${id}`);
  },

  // 重置密码
  resetPassword: (id: number): Promise<string> => {
    return request.put('/system/user/resetPassword', {
      userId: id,
      newPassword: '123456' // 默认密码
    });
  },

  // 修改密码
  changePassword: (data: ChangePasswordRequest): Promise<string> => {
    return request.put('/system/user/changePassword', data);
  },

  // 分配角色
  assignRoles: (data: AssignRolesRequest): Promise<string> => {
    return request.put('/system/user/assignRoles', data);
  },

  // 获取用户权限
  getUserPermissions: (userId: number) => request.get<number[]>(`/system/user/${userId}/permissions`),

  // 分配用户权限
  assignUserPermissions: (data: AssignUserPermissionsRequest) => request.post('/system/user/assign-permissions', data),
};